<template>
  <div class="container">
    <div class="header"><WorthHeader>网易严选</WorthHeader></div>

    <div class="main">
      <img class="logo" src="https://yanxuan.nosdn.127.net/static-union/164793255107785e.png" alt="" />
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field v-model="register.phone" name="phone" label="手机号" placeholder="请输入手机号" :rules="[{ pattern: phone, required: true, message: '请输入正确的手机号' }]" />
          <van-field v-model="register.code" center label="验证码" name="code" placeholder="请输入短信验证码" clickable :rules="[{ required: true, message: '请输入验证码' }]">
            <template #button>
              <button class="btn" @click="sendcode">发送验证码</button>
            </template>
          </van-field>
          <van-field
            v-model="register.password"
            type="password"
            name="password"
            label="密码"
            placeholder="密码"
            :rules="[{ pattern: password, required: true, message: '请输入密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线)' }]"
          />
        </van-cell-group>
        <van-checkbox v-model="checked" shape="square" icon-size="12px" class="checkbox" click="isChecked">用户勾选即代表同意《网易账号服务条款》</van-checkbox>
        <div style="margin: 16px">
          <van-button round block type="primary" native-type="submit"> 提交 </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  name: 'Registration'
})
</script>

<script lang="ts" setup>
import { reqRegisterOver, reqRegister } from '@/api/modules/Registration'
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { Toast } from 'vant'
import 'vant/es/toast/style/index'
const router = useRouter()

const register = ref({
  phone: '',
  password: '',
  code: ''
})
const checked = ref(false)

const phone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
// const pattern = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
const password = /^[a-zA-Z]\w{5,17}$/

const onSubmit = async (values: any) => {
  console.log(values)
  console.log(111)
  // console.log(checked)
  const { phone, password, code } = values
  try {
    await reqRegisterOver({ phone, password, code })
    Toast.success('注册成功')
    router.push({
      path: '/item/login'
    })
  } catch (e) {
    Toast.fail('注册失败')
  }
}

const isChecked = () => {
  checked.value = !checked.value
}

const sendcode = async () => {
  try {
    const res = await reqRegister(register.value.phone)
    console.log(res)
  } catch (e) {
    Toast.fail('获取code失败')
  }
}
</script>
<style scoped>
.container {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.container .header {
  width: 100%;
  flex-grow: 1;
}
.container .main {
  height: 245px;
}
.main .logo {
  width: 134px;
  height: 45px;
  margin-top: 40px;
  margin-bottom: 80px;
  margin-left: 120px;
}
.checkbox {
  font-size: var(--van-font-size-md);
  padding-left: var(--van-padding-lg);
  padding-top: var(--van-padding-lg);
}
.btn {
  background-color: #2c82ff;
  color: white;
  border: none;
  border-radius: 30px;
}
</style>
